package br.indt.framework.ui.common;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import br.indt.framework.ui.ImageLoader;
import br.indt.framework.utils.StringUtils;

/**
 * Display a image on screen.
 * 
 * @author Nokia Institute of Technology
 *
 */
public class Icon extends Widget {
	
	private static final String ICON_PATH = "/icon/";
	private Image selectedImage = null;
	protected ObjectActionListener objectActionListener = null;
	
	public Icon(String name, String imageName, String selectedImageName, int backgroundColor){
		super(name);
		
		try {
			this.backgroundColor = backgroundColor;
			if (imageName != null && !imageName.equals(StringUtils.EMPTY)) {
				setBackgroundImage(ImageLoader.load(ICON_PATH + imageName));
			}
			
			if (selectedImageName != null && !selectedImageName.equals(StringUtils.EMPTY)) { 
				this.selectedImage = ImageLoader.load(ICON_PATH + selectedImageName);
			}
		} catch (Exception e) {			
		}
	}

	public void paint(Graphics g, int x, int y, int maxWidth, int maxHeight){
		
		y += topMargin;
		x += leftMargin;
		
		g.setClip(x, y, width, height);
		
		try {
			if (this.backgroundColor != Color.TRANSPARENT) {
				g.setColor(this.backgroundColor);
				g.fillRect(x, y, width, height);
			}
			
			if (pressedDown) {
				if (selectedImage != null) {
					g.drawImage(selectedImage, x, y, Graphics.SOLID);
				}
			} else {
				if (backgroundImage != null) {
					g.drawImage(backgroundImage, x, y, Graphics.SOLID);
				}
			}
	
			g.setClip(x, y, maxWidth, maxHeight);
		} catch(Exception ex) {			
		}
	}

	public void pressFire() {
		
		
	}
	
	public void setBackgroundImage(Image image) {
		super.setBackgroundImage(image);
		if (image != null) {
			setWidth(backgroundImage.getWidth());
			setHeight(backgroundImage.getHeight());
		}
	}

	public Image getSelectedImage() {
		return selectedImage;
	}

	public void setSelectedImage(Image selectedImage) {
		this.selectedImage = selectedImage;
	}
	public void setObjectActionListener(ObjectActionListener objectActionListener) {
		this.objectActionListener = objectActionListener;
	}
	
	public void click(int x, int y) {
		System.out.println("pressfire ----icon");
		if (objectActionListener != null){
			objectActionListener.objetcClicked(0, 0, this);
		}
	}
}